﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AresCustomYacht.Code;

namespace AresCustomYacht
{
    public partial class Track : PageManager
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            InitializePageManager();
            if (Page.IsPostBack) return;


            Utility.ApplyMode((PageMode) _mode, new List<WebControl> { btnAdd, btnAddNShowReport }, new List<WebControl> { btnUpdate }, new List<WebControl> { btnDelete });
            Utility.DisplayPageMessage(this, lblMessage);

            //Get boatId anyway
            var boat = _bl.GetBoat(BoatId);

            dfnBoatId.Text = BoatIdString;
            lblBoatName.Text = boat.BoatName;

            var trackIdString = Utility.GetQueryStringItem(this, "TrackId");

               
            if (!string.IsNullOrEmpty(trackIdString) && _mode == 1) // Process update
            {
                var trackId = int.Parse(trackIdString);
                var track = _bl.GetTrack(trackId);
                ReflectionCopier<Tracking>.CopyObjectToControls(track, dfnTaskId.Parent);
                dfnTrackingId.Text = track.TrackingId.ToString();
                dfnTaskId.Text = track.TaskId.ToString();
                //TODO: Check what is not being loaded here
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var trackId = int.Parse(Utility.GetQueryStringItem(this, "TrackId"));
            var tracking = new Tracking();
            ReflectionCopier<Tracking>.CopyControlsToObject(tracking, dfnTaskId.Parent);
            tracking.TrackingId = int.Parse(dfnTrackingId.Text);
            tracking.BoatId = BoatId;
            tracking.TaskId = int.Parse(dfnTaskId.Text);
            tracking.InvoiceMaterialPercent = dfpInvoiceMaterialPercent.ValueDecimal;
                
            _dal.UpdateTracking(trackId,tracking);
            Response.Redirect("Main-Reports.aspx?k=" + Utility.Encrypt("Mode=0&BoatId=" + BoatIdString + "&Message=Task Updated"));
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Main-Reports.aspx?k=" + Utility.Encrypt("Mode=0&BoatId=" + BoatIdString + "&Message=Cancelled"));
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {

        }

        protected void btnAddNShowReport_Click(object sender, EventArgs e)
        {

        }

        protected void dfsTaskDescription_TextChanged(object sender, EventArgs e)
        {

        }
    }
}